<?php
    class Animus_admin_m extends Model{
        function Animus_admin_m(){
            parent::Model();
            $this->load->database();
        }
        function get_all_annonce_types(){
            return $this->db->get('animus_anounce_level')->result_array();
        }
        function get_all_annonces($level,$per_page, $off_set){
            $this->db->select('animus_annonce.id,animus_annonce.title,
            animus_annonce.description,animus_annonce.posted_by,
            animus_annonce.date_posted,animus_annonce.images,
            animus_annonce.active,
            animus_annonce.date_expried,lehu_users.username AS username');
            $this->db->from('animus_annonce');
            $this->db->join('lehu_users', 'animus_annonce.posted_by = lehu_users.id');
            if($level>0){
                $this->db->where('animus_annonce.level', $level);
            }
            $this->db->limit($per_page,$off_set);
            return $this->db->get()->result_array();
        }
        function count_all_annonces($level){
            $this->db->select();
            $this->db->from('animus_annonce');
            $this->db->join('lehu_users', 'animus_annonce.posted_by = lehu_users.id');
            if($level>0){
                $this->db->where('animus_annonce.level', $level);
            }
            return count($this->db->get()->result_array());
        }
        function delete_annonce($id){
            return $this->db->delete('animus_annonce', array('id' => $id));  
        }
        function set_activation_status($annonce_id){
            $original_status = $this->db->get_where('animus_annonce',array('id'=>$annonce_id))->row()->active;
            $new_status = $original_status==0?1:0;
            return $this->db->update('animus_annonce',array('active'=>$new_status),array('id'=>$annonce_id));    
        }
        function get_annonce_detail($id){
            $this->db->select('animus_annonce.id,
            animus_annonce.title,
            animus_annonce.description,
            animus_annonce.content,
            animus_annonce.images,
            animus_annonce.date_posted,
            animus_annonce.date_expried,
            animus_annonce.posted_by as userid,
            animus_annonce.level, 
            animus_sub_region.`id` AS sub_region_id,
            animus_sub_region.`name` AS sub_region_name,
            lehu_users.username');
            $this->db->from('animus_annonce ,animus_sub_region ,lehu_users');
            $this->db->where('animus_annonce.posted_by = lehu_users.id');
            $this->db->where('animus_annonce.sub_region = animus_sub_region.id');
            $this->db->where('animus_annonce.id', $id);
            $query = $this->db->get();
            return $query;
        }
        function modiier_annonce($posted_data,$additional_data,$id){
            $data = array();
            $animus_annonce_table = array('title','description','content','images','date_posted','posted_by','date_edited','edited_by','active');
            $original_level = $this->db->get_where('animus_annonce',array('id'=>$id))->row()->level;
            if(!empty($posted_data)){
                foreach($animus_annonce_table as $input){
                    if(is_array($posted_data)&&isset($posted_data[$input])){
                        $data[$input] =  $posted_data[$input];
                    }
                    if(!empty($additional_data)&&isset($additional_data[$input])){
                        $data[$input] =  $additional_data[$input];
                    }
                }
                if($original_level==$posted_data['level']){
                    $data['date_expried'] = $additional_data['date_expried'];
                } else{
                    $data['level'] = $posted_data['level'];
                    //$now_date_string = mdate("%d-%m-%Y %H:%i",time());
                    $day_allowed = $this->db->get_where('animus_anounce_level',array('id'=>$data['level']))->row()->day_allow;
                    $dc = new DateClass();
                    $dc->Add("days", (int)$day_allowed);
                    $string_date = $dc->ToString();
                    $data['date_expried'] = human_to_unix($string_date); 
                }
            }
            return $this->db->update('animus_annonce',$data,array('id'=>$id));
        }
        function get_all_level(){
            $this->db->select();
            $this->db->from('animus_anounce_level');
            return $this->db->get()->result_array();
        }
        function get_level_by_id($id){
            $this->db->select();
            $this->db->from('animus_anounce_level');
            $this->db->where('id',$id);
            return $this->db->get(); 
        }
        function update_level($data,$id){
            return $this->db->update('animus_anounce_level',$data,array('id'=>$id));
        }    
    }
?>
